package com.cssw.fyzb.core.dao.user;

import com.cssw.fyzb.core.model.dodb.UserSource;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class UserSourceDaoImpl implements UserSourceDao {
    @Override
    public UserSource getBySourceCode(String sourceCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("sourceCode", sourceCode);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM t_user_source");
        sqlParse.addSQL(" WHERE source_code =:sourceCode");
        return UserSource.dao.findFirst(sqlParse);
    }

    @Override
    public UserSource getBySourceCode(Integer sourceType, String sourceCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("sourceCode", sourceCode);
        param.put("sourceType", sourceType);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" SELECT * FROM t_user_source ");
        sqlParse.addSQL("  WHERE source_code =:sourceCode ");
        sqlParse.addSQL(" and source_type =:sourceType ");

        return UserSource.dao.findFirst(sqlParse);
    }

    @Override
    public UserSource getBySourceCode(Long userId, Integer sourceType) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("sourceType", sourceType);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" SELECT * FROM t_user_source ");
        sqlParse.addSQL("  WHERE user_id =:userId ");
        sqlParse.addSQL(" and source_type =:sourceType ");
        return UserSource.dao.findFirst(sqlParse);
    }

    @Override
    public Long save(Long userId, Integer sourceType, String sourceCode, String sourceName) {
        UserSource userSource = new UserSource();
        userSource.setUserId(userId);
        userSource.setSourceType(sourceType);
        userSource.setSourceCode(sourceCode);
        userSource.setSourceName(sourceName);
        userSource.save();
        return userSource.getUsId();
    }
}
